package com.example.dllo.baidumusic.fragment;
/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         佛祖保佑       永无BUG
*/

import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.dllo.baidumusic.R;
import com.example.dllo.baidumusic.adapter.PaiHangSingAdapter;
import com.example.dllo.baidumusic.bean.HotSingBean;
import com.example.dllo.baidumusic.bean.MusicBean;
import com.example.dllo.baidumusic.bean.PlayBean;
import com.example.dllo.baidumusic.nettool.URLValuas;
import com.example.dllo.baidumusic.events.MusicListEvent;
import com.example.dllo.baidumusic.nettool.BaseFragment;
import com.example.dllo.baidumusic.nettool.NetTool;
//import com.example.dllo.baidumusic.service.PlayService;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/8/19.
 */
public class NewSingFragment extends BaseFragment implements View.OnClickListener {
    private ImageButton imageButton;
    private ListView listView;
    private ImageView imageView;
    private PaiHangSingAdapter adapter;
    private String TOPSING;
    private NetTool netTool;
    private TextView title;


    @Override
    protected int setLayout() {
        return R.layout.fragment_hotsing;
    }

    @Override
    protected void initView() {
        listView = bindView(R.id.sing_listview);
        title = bindView(R.id.sing_text_title);
        listView.addHeaderView(LayoutInflater.from(getContext()).inflate(R.layout.sing_toushitu, null));
        imageView = bindView(R.id.sing_img);
        imageButton = bindView(R.id.sing_btn_fanhui);
        imageButton.setOnClickListener(this);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                List<MusicBean> musicList = getMusicBeanFromArray();

                //传值
                EventBus.getDefault().post(new MusicListEvent(position - 1, musicList));
                //获得播放列表


            }


        });
    }


    private List<MusicBean> getMusicBeanFromArray() {
        List<MusicBean> musicBeanList = new ArrayList<>();
        for (MusicBean musicBean : musicBeen) {
            musicBeanList.add(musicBean);
        }
        return musicBeanList;
    }


    private MusicBean[] musicBeen;//歌曲信息

    @Override
    protected void initData() {
        adapter = new PaiHangSingAdapter(getContext());
        TOPSING = getArguments().getString("sing");
        netTool = new NetTool();
        netTool.getNetData(TOPSING, HotSingBean.class, new NetTool.NetListener<HotSingBean>() {
            @Override
            public void onSuccess(HotSingBean hotSingBean) {
                title.setText(hotSingBean.getBillboard().getName());
                netTool.getImage(hotSingBean.getBillboard().getPic_s640(), imageView);
                adapter.setHotSingBean(hotSingBean);
                listView.setAdapter(adapter);

                musicBeen = new MusicBean[hotSingBean.getSong_list().size()];

                for (int i = 0; i < hotSingBean.getSong_list().size(); i++) {
                    getMusicInfo(i, URLValuas.PLAY_FRONT + hotSingBean.getSong_list()
                            .get(i).getSong_id() + URLValuas.PLAY_BEHIND);
                }
            }

            @Override
            public void onError(String errorMsg) {

            }
        });

    }


    private void getMusicInfo(final int pos, final String url) {
        netTool.getNetData(url, PlayBean.class, new NetTool.NetListener<PlayBean>() {
            @Override
            public void onSuccess(PlayBean playBean) {
                MusicBean musicBean = new MusicBean();
                try {
                    musicBean.setUrl(playBean.getBitrate().getFile_link());//获取 歌曲网址
                    musicBean.setMusicName(playBean.getSonginfo().getTitle());//歌名
                    musicBean.setSingerPic(playBean.getSonginfo().getPic_small());//图片
                    musicBean.setSingerName(playBean.getSonginfo().getAuthor());//歌手
                    musicBean.setBigpic(playBean.getSonginfo().getPic_huge());//da tu pian
                    musicBean.setTitle(playBean.getSonginfo().getAlbum_title());
                    musicBean.setLrcUrl(playBean.getSonginfo().getLrclink());
//                    Log.d("NewSingFragment", playBean.getSonginfo().getLrclink());
                    musicBeen[pos] = musicBean;//添加到集合
                } catch (Exception e) {
//                    Log.d("NewSingFragment", "pos:" + pos);
//                    musicBean.setBigpic(playBean.getSonginfo().getPic_big());
                }


            }

            @Override
            public void onError(String errorMsg) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        getFragmentManager().popBackStack();//点击 返回上一页
    }
}
